﻿/*
Copyright (C) 2009 Dieter Vandroemme


Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

using System;
using System.Text;

namespace RandomUtils {
    /// <summary>
    /// <para>A simple structure for representing a tree of strings (Value).</para>
    /// <para>New string trees can be added as nodes of this one.</para>
    /// <para>You should only have a string value at the leaf nodes.</para>
    /// <para>You can use this as an Abstract Syntax Tree for your own lexing logic.</para>
    /// </summary>
    public class StringTree {

        #region Fields
        private string _value, _childDelimiter;
        private StringTree[] _childs;
        #endregion

        #region Properties
        /// <summary>
        /// Preferably, you only have values for leaf nodes.
        /// </summary>
        public string Value { get { return _value; } }
        /// <summary>
        /// When calling ComineValues() the child delimiter is used at eache node level to combine the leaf node values.
        /// </summary>
        public string ChildDelimiter { get { return _childDelimiter; } }
        /// <summary>
        /// Indexer for this StringTree.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public StringTree this[int index] {
            get { return _childs[index]; }
            set { _childs[index] = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int Count { get { return _childs.Length; } }
        #endregion

        #region Constructors

        /// <summary>
        /// <para>A simple structure for representing a tree of strings (Value).</para>
        /// <para>New string trees can be added as nodes of this one.</para>
        /// <para>You should only have a string value at the leaf nodes.</para>
        /// <para>You can use this as an Abstract Syntax Tree for your own lexing logic.</para>
        /// </summary>
        /// <param name="value"></param>
        /// <param name="childDelimiter">Use combine values to return a string representation of the elements in this structure.</param>
        /// <param name="count">Set the number of child nodes.</param>
        public StringTree(string value = "", string childDelimiter = "", int count = 0) {
            if (value == null)
                throw new ArgumentNullException("value");
            if (childDelimiter == null)
                throw new ArgumentNullException("childDelimiter");

            _childs = new StringTree[count];

            _value = value;
            _childDelimiter = childDelimiter;
        }

        #endregion

        #region Functions

        /// <summary>
        /// This combines all the leaf node values with all the child delimiters. 
        /// </summary>
        /// <returns>If this has no childs the plain value is returned.</returns>
        public string CombineValues() {
            if (_childs.Length == 0)
                return _value;

            var sb = new StringBuilder();
            if (_childDelimiter.Length == 0) {
                sb.Append(this[0].CombineValues());
            } else {
                for (int i = 0; i < _childs.Length - 1; i++) {
                    sb.Append(this[i].CombineValues());
                    sb.Append(_childDelimiter);
                }
                sb.Append((this[_childs.Length - 1]).CombineValues());
            }
            return sb.ToString();
        }
        /// <summary>
        /// return base.ToString() + (_childs.Length == 0 ? " value: " + _value : " count: " + _childs.Length).
        /// </summary>
        /// <returns></returns>
        public override string ToString() {
            return base.ToString() + (_childs.Length == 0 ? " value: " + _value : " count: " + _childs.Length);
        }

        #endregion
    }
}